home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8794 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.3 KB  |  90 lines

  1. Path: gail.ripco.com!mambuhl
  2. From: mambuhl@ripco.com (Martin Ambuhl)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Very Newbie question
  5. Date: 6 Mar 1996 10:19:37 GMT
  6. Organization: Ripco Communications, Inc.
  7. Message-ID: <4hjorp$2dj@gail.ripco.com>
  8. NNTP-Posting-Host: golden.ripco.com
  9.  
  10. bantolov@bantolov.seanet.com (Bruce Antolovich) in
  11. <bantolov-0603960018080001@bantolov.seanet.com> writes:
  12.  
  13. >I am very new to programming in C but have a large background in FORTRAN.
  14. >(please no snickers) ...
  15.  
  16. There's no reason for snickers.  Fortran is still the right answer for
  17. many problems (so don't abandon it).
  18.  
  19. >...I am having a problem with the memory location of
  20. >variables as described by pointers. I define several variables and try to
  21. >get their address in memory. Unfortunately, my code gives me the same
  22. >address for all variables! Any clues as to where I'm going wrong would be
  23. >very appreciated.
  24.  
  25. The changes to your printf statements in the slight revision of your
  26. code should answer your questions.  In it, as usual, my comments are of
  27. the form `/* mha - ... */'.
  28.  
  29. >I don't think that it matters but I'm using Metrowerks CW on a PowerMac 6100/60
  30.  
  31. Thanks for the info, but you are right:  this is a pure C problem.
  32.  
  33. #include <stdio.h>
  34.  
  35. /***********************/
  36. /* Function Prototypes */
  37. /***********************/
  38. void SquareIt(int number, int *squarePtr, int *cubePtr);
  39.  
  40. int main(void)
  41. {
  42.     int square;
  43.     int cube;
  44.     int *myCube;
  45.     int *mySquare;
  46.     double test;
  47.     mySquare = □
  48.     myCube = &cube;
  49.     printf("%p \n", (void *) &test);    /* mha - added (void *) and
  50.                                          * changed "%d" (int format) to
  51.                                          * "%p" (pointer format) here
  52.                                          * and in the next two
  53.                                          * printf()s and the 1st two in
  54.                                          * SquareIt().  There is no
  55.                                          * guaranteed relationship
  56.                                          * between an int and a
  57.                                          * pointer, and the pointers
  58.                                          * should be cast to (void *)
  59.                                          * since that is what "%p"
  60.                                          * expects and, because
  61.                                          * printf() is a variadic
  62.                                          * function, there is no
  63.                                          * conversion implied for the
  64.                                          * pointers by the prototype */
  65.     printf("The address of the variable square is %p. \n", (void *) mySquare);
  66.     printf("The address of the variable cube is %p. \n", (void *) myCube);
  67.     SquareIt(5, &square, &cube);
  68.     printf("5 squared is %d.\n", square);
  69.     printf("5 cubed is %d. \n", cube);
  70.     return 0;
  71. }
  72.  
  73. void SquareIt(int number, int *squarePtr, int *cubePtr)
  74. {
  75.     printf("squarePtr is %p. (the address of the calling variable)\n ",
  76.            (void *) squarePtr); /* mha - see the comments above for
  77.                                  * those printf()s */
  78.     printf("cubePtr is %p. (the address of the calling variable) \n",
  79.            (void *) cubePtr);
  80.  
  81.     *squarePtr = number * number;
  82.     printf("%d \n", *squarePtr);
  83.     *cubePtr = number * (*squarePtr);
  84. }
  85.  
  86.      
  87. --
  88. * Martin Ambuhl       net: mambuhl@ripco.com
  89. * Chicago, IL (USA)    
  90.